1 From 576623d706613feec2392ef16b84e23a46200552 Mon Sep 17 00:00:00 2001
2 From: AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>
3 Date: Fri, 1 Mar 2024 14:29:24 +0100
4 Subject: [PATCH] pinctrl: Add driver for Awinic AW9523/B I2C GPIO Expander
6 The Awinic AW9523(B) is a multi-function I2C gpio expander in a
7 TQFN-24L package, featuring PWM (max 37mA per pin, or total max
8 power 3.2Watts) for LED driving capability.
10 It has two ports with 8 pins per port (for a total of 16 pins),
11 configurable as either PWM with 1/256 stepping or GPIO input/output,
12 1.8V logic input; each GPIO can be configured as input or output
13 independently from each other.
15 This IC also has an internal interrupt controller, which is capable
16 of generating an interrupt for each GPIO, depending on the
17 configuration, and will raise an interrupt on the INTN pin to
18 advertise this to an external interrupt controller.
20 Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>
21 Signed-off-by: David Bauer <mail@david-bauer.net>
22 Link: https://lore.kernel.org/r/20210624214458.68716-2-mail@david-bauer.net
23 Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
24 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
25 Link: https://lore.kernel.org/r/20240301-awinic-aw9523-v8-1-7ec572f5dfb4@linaro.org
27 drivers/pinctrl/Kconfig | 18 +
28 drivers/pinctrl/Makefile | 1 +
29 drivers/pinctrl/pinctrl-aw9523.c | 1118 ++++++++++++++++++++++++++++++
30 3 files changed, 1137 insertions(+)
31 create mode 100644 drivers/pinctrl/pinctrl-aw9523.c
33 --- a/drivers/pinctrl/Kconfig
34 +++ b/drivers/pinctrl/Kconfig
35 @@ -127,6 +127,24 @@ config PINCTRL_AXP209
37 Say Y to enable pinctrl and GPIO support for the AXP209 PMIC.
39 +config PINCTRL_AW9523
40 + bool "Awinic AW9523/AW9523B I2C GPIO expander pinctrl driver"
41 + depends on OF && I2C
44 + select GENERIC_PINCONF
46 + select GPIOLIB_IRQCHIP
50 + The Awinic AW9523/AW9523B is a multi-function I2C GPIO
51 + expander with PWM functionality. This driver bundles a
52 + pinctrl driver to select the function muxing and a GPIO
53 + driver to handle GPIO, when the GPIO function is selected.
55 + Say yes to enable pinctrl and GPIO support for the AW9523(B).
58 bool "Bitmain BM1880 Pinctrl driver"
59 depends on OF && (ARCH_BITMAIN || COMPILE_TEST)
60 --- a/drivers/pinctrl/Makefile
61 +++ b/drivers/pinctrl/Makefile
62 @@ -15,6 +15,7 @@ obj-$(CONFIG_PINCTRL_ARTPEC6) += pinctrl
63 obj-$(CONFIG_PINCTRL_AS3722) += pinctrl-as3722.o
64 obj-$(CONFIG_PINCTRL_AT91) += pinctrl-at91.o
65 obj-$(CONFIG_PINCTRL_AT91PIO4) += pinctrl-at91-pio4.o
66 +obj-$(CONFIG_PINCTRL_AW9523) += pinctrl-aw9523.o
67 obj-$(CONFIG_PINCTRL_AXP209) += pinctrl-axp209.o
68 obj-$(CONFIG_PINCTRL_BM1880) += pinctrl-bm1880.o
69 obj-$(CONFIG_PINCTRL_CY8C95X0) += pinctrl-cy8c95x0.o
71 +++ b/drivers/pinctrl/pinctrl-aw9523.c
73 +// SPDX-License-Identifier: GPL-2.0-only
75 + * Awinic AW9523B i2c pin controller driver
76 + * Copyright (c) 2020, AngeloGioacchino Del Regno
77 + * <angelogioacchino.delregno@somainline.org>
80 +#include <linux/bitfield.h>
81 +#include <linux/gpio/consumer.h>
82 +#include <linux/gpio/driver.h>
83 +#include <linux/i2c.h>
84 +#include <linux/init.h>
85 +#include <linux/interrupt.h>
86 +#include <linux/irq.h>
87 +#include <linux/mutex.h>
88 +#include <linux/module.h>
89 +#include <linux/pinctrl/pinconf.h>
90 +#include <linux/pinctrl/pinctrl.h>
91 +#include <linux/pinctrl/pinmux.h>
92 +#include <linux/pinctrl/pinconf-generic.h>
93 +#include <linux/property.h>
94 +#include <linux/regmap.h>
95 +#include <linux/regulator/consumer.h>
96 +#include <linux/slab.h>
98 +#define AW9523_MAX_FUNCS 2
99 +#define AW9523_NUM_PORTS 2
100 +#define AW9523_PINS_PER_PORT 8
103 + * HW needs at least 20uS for reset and at least 1-2uS to recover from
104 + * reset, but we have to account for eventual board quirks, if any:
105 + * for this reason, keep reset asserted for 50uS and wait for 20uS
106 + * to recover from the reset.
108 +#define AW9523_HW_RESET_US 50
109 +#define AW9523_HW_RESET_RECOVERY_US 20
111 +/* Port 0: P0_0...P0_7 - Port 1: P1_0...P1_7 */
112 +#define AW9523_PIN_TO_PORT(pin) (pin >> 3)
113 +#define AW9523_REG_IN_STATE(pin) (0x00 + AW9523_PIN_TO_PORT(pin))
114 +#define AW9523_REG_OUT_STATE(pin) (0x02 + AW9523_PIN_TO_PORT(pin))
115 +#define AW9523_REG_CONF_STATE(pin) (0x04 + AW9523_PIN_TO_PORT(pin))
116 +#define AW9523_REG_INTR_DIS(pin) (0x06 + AW9523_PIN_TO_PORT(pin))
117 +#define AW9523_REG_CHIPID 0x10
118 +#define AW9523_VAL_EXPECTED_CHIPID 0x23
120 +#define AW9523_REG_GCR 0x11
121 +#define AW9523_GCR_ISEL_MASK GENMASK(0, 1)
122 +#define AW9523_GCR_GPOMD_MASK BIT(4)
124 +#define AW9523_REG_PORT_MODE(pin) (0x12 + AW9523_PIN_TO_PORT(pin))
125 +#define AW9523_REG_SOFT_RESET 0x7f
126 +#define AW9523_VAL_RESET 0x00
129 + * struct aw9523_irq - Interrupt controller structure
130 + * @lock: mutex locking for the irq bus
131 + * @irqchip: structure holding irqchip params
132 + * @cached_gpio: stores the previous gpio status for bit comparison
136 + struct irq_chip *irqchip;
141 + * struct aw9523_pinmux - Pin mux params
142 + * @name: Name of the mux
143 + * @grps: Groups of the mux
144 + * @num_grps: Number of groups (sizeof array grps)
146 +struct aw9523_pinmux {
148 + const char * const *grps;
153 + * struct aw9523 - Main driver structure
154 + * @dev: device handle
155 + * @regmap: regmap handle for current device
156 + * @i2c_lock: Mutex lock for i2c operations
157 + * @reset_gpio: Hardware reset (RSTN) signal GPIO
158 + * @vio_vreg: VCC regulator (Optional)
159 + * @pctl: pinctrl handle for current device
160 + * @gpio: structure holding gpiochip params
161 + * @irq: Interrupt controller structure
164 + struct device *dev;
165 + struct regmap *regmap;
166 + struct mutex i2c_lock;
167 + struct gpio_desc *reset_gpio;
168 + struct regulator *vio_vreg;
169 + struct pinctrl_dev *pctl;
170 + struct gpio_chip gpio;
171 + struct aw9523_irq *irq;
174 +static const struct pinctrl_pin_desc aw9523_pins[] = {
176 + PINCTRL_PIN(0, "gpio0"),
177 + PINCTRL_PIN(1, "gpio1"),
178 + PINCTRL_PIN(2, "gpio2"),
179 + PINCTRL_PIN(3, "gpio3"),
180 + PINCTRL_PIN(4, "gpio4"),
181 + PINCTRL_PIN(5, "gpio5"),
182 + PINCTRL_PIN(6, "gpio6"),
183 + PINCTRL_PIN(7, "gpio7"),
186 + PINCTRL_PIN(8, "gpio8"),
187 + PINCTRL_PIN(9, "gpio9"),
188 + PINCTRL_PIN(10, "gpio10"),
189 + PINCTRL_PIN(11, "gpio11"),
190 + PINCTRL_PIN(12, "gpio12"),
191 + PINCTRL_PIN(13, "gpio13"),
192 + PINCTRL_PIN(14, "gpio14"),
193 + PINCTRL_PIN(15, "gpio15"),
196 +static int aw9523_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
198 + return ARRAY_SIZE(aw9523_pins);
201 +static const char *aw9523_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
202 + unsigned int selector)
204 + return aw9523_pins[selector].name;
207 +static int aw9523_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
208 + unsigned int selector,
209 + const unsigned int **pins,
210 + unsigned int *num_pins)
212 + *pins = &aw9523_pins[selector].number;
217 +static const struct pinctrl_ops aw9523_pinctrl_ops = {
218 + .get_groups_count = aw9523_pinctrl_get_groups_count,
219 + .get_group_pins = aw9523_pinctrl_get_group_pins,
220 + .get_group_name = aw9523_pinctrl_get_group_name,
221 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
222 + .dt_free_map = pinconf_generic_dt_free_map,
225 +static const char * const gpio_pwm_groups[] = {
226 + "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5",
227 + "gpio6", "gpio7", "gpio8", "gpio9", "gpio10", "gpio11",
228 + "gpio12", "gpio13", "gpio14", "gpio15"
231 +/* Warning: Do NOT reorder this array */
232 +static const struct aw9523_pinmux aw9523_pmx[] = {
235 + .grps = gpio_pwm_groups,
236 + .num_grps = ARRAY_SIZE(gpio_pwm_groups),
240 + .grps = gpio_pwm_groups,
241 + .num_grps = ARRAY_SIZE(gpio_pwm_groups),
245 +static int aw9523_pmx_get_funcs_count(struct pinctrl_dev *pctl)
247 + return ARRAY_SIZE(aw9523_pmx);
250 +static const char *aw9523_pmx_get_fname(struct pinctrl_dev *pctl,
253 + return aw9523_pmx[sel].name;
256 +static int aw9523_pmx_get_groups(struct pinctrl_dev *pctl, unsigned int sel,
257 + const char * const **groups,
258 + unsigned int * const num_groups)
260 + *groups = aw9523_pmx[sel].grps;
261 + *num_groups = aw9523_pmx[sel].num_grps;
265 +static int aw9523_pmx_set_mux(struct pinctrl_dev *pctl, unsigned int fsel,
268 + struct aw9523 *awi = pinctrl_dev_get_drvdata(pctl);
269 + int ret, pin = aw9523_pins[grp].number % AW9523_PINS_PER_PORT;
271 + if (fsel >= ARRAY_SIZE(aw9523_pmx))
275 + * This maps directly to the aw9523_pmx array: programming a
276 + * high bit means "gpio" and a low bit means "pwm".
278 + mutex_lock(&awi->i2c_lock);
279 + ret = regmap_update_bits(awi->regmap, AW9523_REG_PORT_MODE(pin),
280 + BIT(pin), (fsel ? BIT(pin) : 0));
281 + mutex_unlock(&awi->i2c_lock);
285 +static const struct pinmux_ops aw9523_pinmux_ops = {
286 + .get_functions_count = aw9523_pmx_get_funcs_count,
287 + .get_function_name = aw9523_pmx_get_fname,
288 + .get_function_groups = aw9523_pmx_get_groups,
289 + .set_mux = aw9523_pmx_set_mux,
292 +static int aw9523_pcfg_param_to_reg(enum pin_config_param pcp, int pin, u8 *r)
297 + case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
298 + case PIN_CONFIG_BIAS_PULL_DOWN:
299 + case PIN_CONFIG_BIAS_PULL_UP:
300 + reg = AW9523_REG_IN_STATE(pin);
302 + case PIN_CONFIG_DRIVE_OPEN_DRAIN:
303 + case PIN_CONFIG_DRIVE_PUSH_PULL:
304 + reg = AW9523_REG_GCR;
306 + case PIN_CONFIG_INPUT_ENABLE:
307 + case PIN_CONFIG_OUTPUT_ENABLE:
308 + reg = AW9523_REG_CONF_STATE(pin);
310 + case PIN_CONFIG_OUTPUT:
311 + reg = AW9523_REG_OUT_STATE(pin);
314 + return -EOPNOTSUPP;
321 +static int aw9523_pconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
322 + unsigned long *config)
324 + struct aw9523 *awi = pinctrl_dev_get_drvdata(pctldev);
325 + enum pin_config_param param = pinconf_to_config_param(*config);
326 + int regbit = pin % AW9523_PINS_PER_PORT;
331 + rc = aw9523_pcfg_param_to_reg(param, pin, ®);
335 + mutex_lock(&awi->i2c_lock);
336 + rc = regmap_read(awi->regmap, reg, &val);
337 + mutex_unlock(&awi->i2c_lock);
342 + case PIN_CONFIG_BIAS_PULL_UP:
343 + case PIN_CONFIG_INPUT_ENABLE:
344 + case PIN_CONFIG_OUTPUT:
345 + val &= BIT(regbit);
347 + case PIN_CONFIG_BIAS_PULL_DOWN:
348 + case PIN_CONFIG_OUTPUT_ENABLE:
349 + val &= BIT(regbit);
352 + case PIN_CONFIG_DRIVE_OPEN_DRAIN:
353 + if (pin >= AW9523_PINS_PER_PORT)
356 + val = !FIELD_GET(AW9523_GCR_GPOMD_MASK, val);
358 + case PIN_CONFIG_DRIVE_PUSH_PULL:
359 + if (pin >= AW9523_PINS_PER_PORT)
362 + val = FIELD_GET(AW9523_GCR_GPOMD_MASK, val);
365 + return -EOPNOTSUPP;
370 + *config = pinconf_to_config_packed(param, !!val);
375 +static int aw9523_pconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
376 + unsigned long *configs, unsigned int num_configs)
378 + struct aw9523 *awi = pinctrl_dev_get_drvdata(pctldev);
379 + enum pin_config_param param;
380 + int regbit = pin % AW9523_PINS_PER_PORT;
383 + unsigned int mask, val;
386 + mutex_lock(&awi->i2c_lock);
387 + for (i = 0; i < num_configs; i++) {
388 + param = pinconf_to_config_param(configs[i]);
389 + arg = pinconf_to_config_argument(configs[i]);
391 + rc = aw9523_pcfg_param_to_reg(param, pin, ®);
396 + case PIN_CONFIG_OUTPUT:
397 + /* First, enable pin output */
398 + rc = regmap_update_bits(awi->regmap,
399 + AW9523_REG_CONF_STATE(pin),
404 + /* Then, fall through to config output level */
406 + case PIN_CONFIG_OUTPUT_ENABLE:
409 + case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
410 + case PIN_CONFIG_BIAS_PULL_DOWN:
411 + case PIN_CONFIG_BIAS_PULL_UP:
412 + case PIN_CONFIG_INPUT_ENABLE:
413 + mask = BIT(regbit);
414 + val = arg ? BIT(regbit) : 0;
416 + case PIN_CONFIG_DRIVE_OPEN_DRAIN:
417 + /* Open-Drain is supported only on port 0 */
418 + if (pin >= AW9523_PINS_PER_PORT) {
422 + mask = AW9523_GCR_GPOMD_MASK;
425 + case PIN_CONFIG_DRIVE_PUSH_PULL:
426 + /* Port 1 is always Push-Pull */
427 + if (pin >= AW9523_PINS_PER_PORT) {
432 + mask = AW9523_GCR_GPOMD_MASK;
433 + val = AW9523_GCR_GPOMD_MASK;
440 + rc = regmap_update_bits(awi->regmap, reg, mask, val);
445 + mutex_unlock(&awi->i2c_lock);
449 +static const struct pinconf_ops aw9523_pinconf_ops = {
450 + .pin_config_get = aw9523_pconf_get,
451 + .pin_config_set = aw9523_pconf_set,
452 + .is_generic = true,
456 + * aw9523_get_pin_direction - Get pin direction
457 + * @regmap: Regmap structure
458 + * @pin: gpiolib pin number
459 + * @n: pin index in port register
461 + * Return: Pin direction for success or negative number for error
463 +static int aw9523_get_pin_direction(struct regmap *regmap, u8 pin, u8 n)
467 + ret = regmap_test_bits(regmap, AW9523_REG_CONF_STATE(pin), BIT(n));
471 + return ret ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
475 + * aw9523_get_port_state - Get input or output state for entire port
476 + * @regmap: Regmap structure
477 + * @pin: gpiolib pin number
478 + * @regbit: hw pin index, used to retrieve port number
479 + * @state: returned port state
481 + * Return: Zero for success or negative number for error
483 +static int aw9523_get_port_state(struct regmap *regmap, u8 pin,
484 + u8 regbit, unsigned int *state)
489 + dir = aw9523_get_pin_direction(regmap, pin, regbit);
493 + if (dir == GPIO_LINE_DIRECTION_IN)
494 + reg = AW9523_REG_IN_STATE(pin);
496 + reg = AW9523_REG_OUT_STATE(pin);
498 + return regmap_read(regmap, reg, state);
501 +static int aw9523_gpio_irq_type(struct irq_data *d, unsigned int type)
504 + case IRQ_TYPE_NONE:
505 + case IRQ_TYPE_EDGE_BOTH:
513 + * aw9523_irq_mask - Mask interrupt
516 + * Sets which interrupt to mask in the bitmap;
517 + * The interrupt will be masked when unlocking the irq bus.
519 +static void aw9523_irq_mask(struct irq_data *d)
521 + struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
522 + unsigned int n = d->hwirq % AW9523_PINS_PER_PORT;
524 + regmap_update_bits(awi->regmap,
525 + AW9523_REG_INTR_DIS(d->hwirq),
527 + gpiochip_disable_irq(&awi->gpio, irqd_to_hwirq(d));
531 + * aw9523_irq_unmask - Unmask interrupt
534 + * Sets which interrupt to unmask in the bitmap;
535 + * The interrupt will be masked when unlocking the irq bus.
537 +static void aw9523_irq_unmask(struct irq_data *d)
539 + struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
540 + unsigned int n = d->hwirq % AW9523_PINS_PER_PORT;
542 + gpiochip_enable_irq(&awi->gpio, irqd_to_hwirq(d));
543 + regmap_update_bits(awi->regmap,
544 + AW9523_REG_INTR_DIS(d->hwirq),
548 +static irqreturn_t aw9523_irq_thread_func(int irq, void *dev_id)
550 + struct aw9523 *awi = (struct aw9523 *)dev_id;
551 + unsigned long n, val = 0;
552 + unsigned long changed_gpio;
553 + unsigned int tmp, port_pin, i, ret;
555 + for (i = 0; i < AW9523_NUM_PORTS; i++) {
556 + port_pin = i * AW9523_PINS_PER_PORT;
557 + ret = regmap_read(awi->regmap,
558 + AW9523_REG_IN_STATE(port_pin),
562 + val |= (u8)tmp << (i * 8);
565 + /* Handle GPIO input release interrupt as well */
566 + changed_gpio = awi->irq->cached_gpio ^ val;
567 + awi->irq->cached_gpio = val;
570 + * To avoid up to four *slow* i2c reads from any driver hooked
571 + * up to our interrupts, just check for the irq_find_mapping
572 + * result: if the interrupt is not mapped, then we don't want
573 + * to care about it.
575 + for_each_set_bit(n, &changed_gpio, awi->gpio.ngpio) {
576 + tmp = irq_find_mapping(awi->gpio.irq.domain, n);
579 + handle_nested_irq(tmp);
582 + return IRQ_HANDLED;
586 + * aw9523_irq_bus_lock - Grab lock for interrupt operation
589 +static void aw9523_irq_bus_lock(struct irq_data *d)
591 + struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
593 + mutex_lock(&awi->irq->lock);
594 + regcache_cache_only(awi->regmap, true);
598 + * aw9523_irq_bus_sync_unlock - Synchronize state and unlock
601 + * Writes the interrupt mask bits (found in the bit map) to the
602 + * hardware, then unlocks the bus.
604 +static void aw9523_irq_bus_sync_unlock(struct irq_data *d)
606 + struct aw9523 *awi = gpiochip_get_data(irq_data_get_irq_chip_data(d));
608 + regcache_cache_only(awi->regmap, false);
609 + regcache_sync(awi->regmap);
610 + mutex_unlock(&awi->irq->lock);
613 +static int aw9523_gpio_get_direction(struct gpio_chip *chip,
614 + unsigned int offset)
616 + struct aw9523 *awi = gpiochip_get_data(chip);
617 + u8 regbit = offset % AW9523_PINS_PER_PORT;
620 + mutex_lock(&awi->i2c_lock);
621 + ret = aw9523_get_pin_direction(awi->regmap, offset, regbit);
622 + mutex_unlock(&awi->i2c_lock);
627 +static int aw9523_gpio_get(struct gpio_chip *chip, unsigned int offset)
629 + struct aw9523 *awi = gpiochip_get_data(chip);
630 + u8 regbit = offset % AW9523_PINS_PER_PORT;
634 + mutex_lock(&awi->i2c_lock);
635 + ret = aw9523_get_port_state(awi->regmap, offset, regbit, &val);
636 + mutex_unlock(&awi->i2c_lock);
640 + return !!(val & BIT(regbit));
644 + * _aw9523_gpio_get_multiple - Get I/O state for an entire port
645 + * @regmap: Regmap structure
646 + * @pin: gpiolib pin number
647 + * @regbit: hw pin index, used to retrieve port number
648 + * @state: returned port I/O state
650 + * Return: Zero for success or negative number for error
652 +static int _aw9523_gpio_get_multiple(struct aw9523 *awi, u8 regbit,
653 + u8 *state, u8 mask)
659 + /* Registers are 8-bits wide */
660 + ret = regmap_read(awi->regmap, AW9523_REG_CONF_STATE(regbit), &dir_in);
667 + ret = regmap_read(awi->regmap, AW9523_REG_IN_STATE(regbit),
671 + *state |= (u8)val & m;
674 + m = mask & ~dir_in;
676 + ret = regmap_read(awi->regmap, AW9523_REG_OUT_STATE(regbit),
680 + *state |= (u8)val & m;
686 +static int aw9523_gpio_get_multiple(struct gpio_chip *chip,
687 + unsigned long *mask,
688 + unsigned long *bits)
690 + struct aw9523 *awi = gpiochip_get_data(chip);
694 + mutex_lock(&awi->i2c_lock);
696 + /* Port 0 (gpio 0-7) */
697 + m = *mask & U8_MAX;
699 + ret = _aw9523_gpio_get_multiple(awi, 0, &state, m);
705 + /* Port 1 (gpio 8-15) */
706 + m = (*mask >> 8) & U8_MAX;
708 + ret = _aw9523_gpio_get_multiple(awi, AW9523_PINS_PER_PORT,
713 + *bits |= (state << 8);
716 + mutex_unlock(&awi->i2c_lock);
720 +static void aw9523_gpio_set_multiple(struct gpio_chip *chip,
721 + unsigned long *mask,
722 + unsigned long *bits)
724 + struct aw9523 *awi = gpiochip_get_data(chip);
725 + u8 mask_lo, mask_hi, bits_lo, bits_hi;
729 + mask_lo = *mask & U8_MAX;
730 + mask_hi = (*mask >> 8) & U8_MAX;
731 + mutex_lock(&awi->i2c_lock);
733 + reg = AW9523_REG_OUT_STATE(AW9523_PINS_PER_PORT);
734 + bits_hi = (*bits >> 8) & U8_MAX;
736 + ret = regmap_write_bits(awi->regmap, reg, mask_hi, bits_hi);
738 + dev_warn(awi->dev, "Cannot write port1 out level\n");
743 + reg = AW9523_REG_OUT_STATE(0);
744 + bits_lo = *bits & U8_MAX;
745 + ret = regmap_write_bits(awi->regmap, reg, mask_lo, bits_lo);
747 + dev_warn(awi->dev, "Cannot write port0 out level\n");
750 + mutex_unlock(&awi->i2c_lock);
753 +static void aw9523_gpio_set(struct gpio_chip *chip,
754 + unsigned int offset, int value)
756 + struct aw9523 *awi = gpiochip_get_data(chip);
757 + u8 regbit = offset % AW9523_PINS_PER_PORT;
759 + mutex_lock(&awi->i2c_lock);
760 + regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset),
761 + BIT(regbit), value ? BIT(regbit) : 0);
762 + mutex_unlock(&awi->i2c_lock);
766 +static int aw9523_direction_input(struct gpio_chip *chip, unsigned int offset)
768 + struct aw9523 *awi = gpiochip_get_data(chip);
769 + u8 regbit = offset % AW9523_PINS_PER_PORT;
772 + mutex_lock(&awi->i2c_lock);
773 + ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset),
774 + BIT(regbit), BIT(regbit));
775 + mutex_unlock(&awi->i2c_lock);
780 +static int aw9523_direction_output(struct gpio_chip *chip,
781 + unsigned int offset, int value)
783 + struct aw9523 *awi = gpiochip_get_data(chip);
784 + u8 regbit = offset % AW9523_PINS_PER_PORT;
787 + mutex_lock(&awi->i2c_lock);
788 + ret = regmap_update_bits(awi->regmap, AW9523_REG_OUT_STATE(offset),
789 + BIT(regbit), value ? BIT(regbit) : 0);
793 + ret = regmap_update_bits(awi->regmap, AW9523_REG_CONF_STATE(offset),
796 + mutex_unlock(&awi->i2c_lock);
800 +static int aw9523_drive_reset_gpio(struct aw9523 *awi)
802 + unsigned int chip_id;
806 + * If the chip is already configured for any reason, then we
807 + * will probably succeed in sending the soft reset signal to
808 + * the hardware through I2C: this operation takes less time
809 + * compared to a full HW reset and it gives the same results.
811 + ret = regmap_write(awi->regmap, AW9523_REG_SOFT_RESET, 0);
815 + dev_dbg(awi->dev, "Cannot execute soft reset: trying hard reset\n");
816 + ret = gpiod_direction_output(awi->reset_gpio, 0);
820 + /* The reset pulse has to be longer than 20uS due to deglitch */
821 + usleep_range(AW9523_HW_RESET_US, AW9523_HW_RESET_US + 1);
823 + ret = gpiod_direction_output(awi->reset_gpio, 1);
827 + /* The HW needs at least 1uS to reliably recover after reset */
828 + usleep_range(AW9523_HW_RESET_RECOVERY_US,
829 + AW9523_HW_RESET_RECOVERY_US + 1);
831 + /* Check the ChipID */
832 + ret = regmap_read(awi->regmap, AW9523_REG_CHIPID, &chip_id);
834 + dev_err(awi->dev, "Cannot read Chip ID: %d\n", ret);
837 + if (chip_id != AW9523_VAL_EXPECTED_CHIPID) {
838 + dev_err(awi->dev, "Bad ChipID; read 0x%x, expected 0x%x\n",
839 + chip_id, AW9523_VAL_EXPECTED_CHIPID);
846 +static int aw9523_hw_reset(struct aw9523 *awi)
848 + int ret, max_retries = 2;
850 + /* Sometimes the chip needs more than one reset cycle */
852 + ret = aw9523_drive_reset_gpio(awi);
856 + } while (max_retries);
861 +static int aw9523_init_gpiochip(struct aw9523 *awi, unsigned int npins)
863 + struct device *dev = awi->dev;
864 + struct gpio_chip *gc = &awi->gpio;
866 + gc->label = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
872 + gc->get_direction = aw9523_gpio_get_direction;
873 + gc->direction_input = aw9523_direction_input;
874 + gc->direction_output = aw9523_direction_output;
875 + gc->get = aw9523_gpio_get;
876 + gc->get_multiple = aw9523_gpio_get_multiple;
877 + gc->set = aw9523_gpio_set;
878 + gc->set_multiple = aw9523_gpio_set_multiple;
879 + gc->set_config = gpiochip_generic_config;
881 + gc->owner = THIS_MODULE;
882 + gc->can_sleep = false;
887 +static const struct irq_chip aw9523_irq_chip = {
889 + .irq_mask = aw9523_irq_mask,
890 + .irq_unmask = aw9523_irq_unmask,
891 + .irq_bus_lock = aw9523_irq_bus_lock,
892 + .irq_bus_sync_unlock = aw9523_irq_bus_sync_unlock,
893 + .irq_set_type = aw9523_gpio_irq_type,
894 + .flags = IRQCHIP_IMMUTABLE,
895 + GPIOCHIP_IRQ_RESOURCE_HELPERS,
898 +static int aw9523_init_irq(struct aw9523 *awi, int irq)
900 + struct device *dev = awi->dev;
901 + struct gpio_irq_chip *girq;
902 + struct irq_chip *irqchip;
905 + if (!device_property_read_bool(dev, "interrupt-controller"))
908 + irqchip = devm_kzalloc(dev, sizeof(*irqchip), GFP_KERNEL);
912 + awi->irq = devm_kzalloc(dev, sizeof(*awi->irq), GFP_KERNEL);
916 + awi->irq->irqchip = irqchip;
917 + mutex_init(&awi->irq->lock);
919 + ret = devm_request_threaded_irq(dev, irq, NULL, aw9523_irq_thread_func,
920 + IRQF_ONESHOT, dev_name(dev), awi);
922 + dev_err(dev, "Failed to request irq %d\n", irq);
926 + girq = &awi->gpio.irq;
927 + gpio_irq_chip_set_chip(girq, &aw9523_irq_chip);
928 + girq->parent_handler = NULL;
929 + girq->num_parents = 0;
930 + girq->parents = NULL;
931 + girq->default_type = IRQ_TYPE_EDGE_BOTH;
932 + girq->handler = handle_simple_irq;
933 + girq->threaded = true;
938 +static bool aw9523_is_reg_hole(unsigned int reg)
940 + return (reg > AW9523_REG_PORT_MODE(AW9523_PINS_PER_PORT) &&
941 + reg < AW9523_REG_SOFT_RESET) ||
942 + (reg > AW9523_REG_INTR_DIS(AW9523_PINS_PER_PORT) &&
943 + reg < AW9523_REG_CHIPID);
946 +static bool aw9523_readable_reg(struct device *dev, unsigned int reg)
948 + /* All available registers (minus holes) can be read */
949 + return !aw9523_is_reg_hole(reg);
952 +static bool aw9523_volatile_reg(struct device *dev, unsigned int reg)
954 + return aw9523_is_reg_hole(reg) ||
955 + reg == AW9523_REG_IN_STATE(0) ||
956 + reg == AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT) ||
957 + reg == AW9523_REG_CHIPID ||
958 + reg == AW9523_REG_SOFT_RESET;
961 +static bool aw9523_writeable_reg(struct device *dev, unsigned int reg)
963 + return !aw9523_is_reg_hole(reg) && reg != AW9523_REG_CHIPID;
966 +static bool aw9523_precious_reg(struct device *dev, unsigned int reg)
968 + /* Reading AW9523_REG_IN_STATE clears interrupt status */
969 + return aw9523_is_reg_hole(reg) ||
970 + reg == AW9523_REG_IN_STATE(0) ||
971 + reg == AW9523_REG_IN_STATE(AW9523_PINS_PER_PORT);
974 +static const struct regmap_config aw9523_regmap = {
979 + .precious_reg = aw9523_precious_reg,
980 + .readable_reg = aw9523_readable_reg,
981 + .volatile_reg = aw9523_volatile_reg,
982 + .writeable_reg = aw9523_writeable_reg,
984 + .cache_type = REGCACHE_FLAT,
985 + .disable_locking = true,
987 + .num_reg_defaults_raw = AW9523_REG_SOFT_RESET,
990 +static int aw9523_hw_init(struct aw9523 *awi)
992 + u8 p1_pin = AW9523_PINS_PER_PORT;
996 + /* No register caching during initialization */
997 + regcache_cache_bypass(awi->regmap, true);
999 + /* Bring up the chip */
1000 + ret = aw9523_hw_reset(awi);
1002 + dev_err(awi->dev, "HW Reset failed: %d\n", ret);
1007 + * This is the expected chip and it is running: it's time to
1008 + * set a safe default configuration in case the user doesn't
1009 + * configure (all of the available) pins in this chip.
1010 + * P.S.: The writes order doesn't matter.
1013 + /* Set all pins as GPIO */
1014 + ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(0), U8_MAX);
1017 + ret = regmap_write(awi->regmap, AW9523_REG_PORT_MODE(p1_pin), U8_MAX);
1021 + /* Set Open-Drain mode on Port 0 (Port 1 is always P-P) */
1022 + ret = regmap_write(awi->regmap, AW9523_REG_GCR, 0);
1026 + /* Set all pins as inputs */
1027 + ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(0), U8_MAX);
1030 + ret = regmap_write(awi->regmap, AW9523_REG_CONF_STATE(p1_pin), U8_MAX);
1034 + /* Disable all interrupts to avoid unreasoned wakeups */
1035 + ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(0), U8_MAX);
1038 + ret = regmap_write(awi->regmap, AW9523_REG_INTR_DIS(p1_pin), U8_MAX);
1042 + /* Clear setup-generated interrupts by performing a port state read */
1043 + ret = aw9523_get_port_state(awi->regmap, 0, 0, &val);
1046 + ret = aw9523_get_port_state(awi->regmap, p1_pin, 0, &val);
1050 + /* Everything went fine: activate and reinitialize register cache */
1051 + regcache_cache_bypass(awi->regmap, false);
1052 + return regmap_reinit_cache(awi->regmap, &aw9523_regmap);
1055 +static int aw9523_probe(struct i2c_client *client)
1057 + struct device *dev = &client->dev;
1058 + struct pinctrl_desc *pdesc;
1059 + struct aw9523 *awi;
1062 + awi = devm_kzalloc(dev, sizeof(*awi), GFP_KERNEL);
1066 + i2c_set_clientdata(client, awi);
1069 + awi->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1070 + if (IS_ERR(awi->reset_gpio))
1071 + return PTR_ERR(awi->reset_gpio);
1072 + gpiod_set_consumer_name(awi->reset_gpio, "aw9523 reset");
1074 + awi->regmap = devm_regmap_init_i2c(client, &aw9523_regmap);
1075 + if (IS_ERR(awi->regmap))
1076 + return PTR_ERR(awi->regmap);
1078 + awi->vio_vreg = devm_regulator_get_optional(dev, "vio");
1079 + if (IS_ERR(awi->vio_vreg)) {
1080 + if (PTR_ERR(awi->vio_vreg) == -EPROBE_DEFER)
1081 + return -EPROBE_DEFER;
1082 + awi->vio_vreg = NULL;
1084 + ret = regulator_enable(awi->vio_vreg);
1089 + mutex_init(&awi->i2c_lock);
1090 + lockdep_set_subclass(&awi->i2c_lock,
1091 + i2c_adapter_depth(client->adapter));
1093 + pdesc = devm_kzalloc(dev, sizeof(*pdesc), GFP_KERNEL);
1097 + ret = aw9523_hw_init(awi);
1099 + goto err_disable_vregs;
1101 + pdesc->name = dev_name(dev);
1102 + pdesc->owner = THIS_MODULE;
1103 + pdesc->pctlops = &aw9523_pinctrl_ops;
1104 + pdesc->pmxops = &aw9523_pinmux_ops;
1105 + pdesc->confops = &aw9523_pinconf_ops;
1106 + pdesc->pins = aw9523_pins;
1107 + pdesc->npins = ARRAY_SIZE(aw9523_pins);
1109 + ret = aw9523_init_gpiochip(awi, pdesc->npins);
1111 + goto err_disable_vregs;
1113 + if (client->irq) {
1114 + ret = aw9523_init_irq(awi, client->irq);
1116 + goto err_disable_vregs;
1119 + awi->pctl = devm_pinctrl_register(dev, pdesc, awi);
1120 + if (IS_ERR(awi->pctl)) {
1121 + ret = PTR_ERR(awi->pctl);
1122 + dev_err(dev, "Cannot register pinctrl: %d", ret);
1123 + goto err_disable_vregs;
1126 + ret = devm_gpiochip_add_data(dev, &awi->gpio, awi);
1128 + goto err_disable_vregs;
1133 + if (awi->vio_vreg)
1134 + regulator_disable(awi->vio_vreg);
1135 + mutex_destroy(&awi->i2c_lock);
1139 +static void aw9523_remove(struct i2c_client *client)
1141 + struct aw9523 *awi = i2c_get_clientdata(client);
1148 + * If the chip VIO is connected to a regulator that we can turn
1149 + * off, life is easy... otherwise, reinitialize the chip and
1150 + * set the pins to hardware defaults before removing the driver
1151 + * to leave it in a clean, safe and predictable state.
1153 + if (awi->vio_vreg) {
1154 + regulator_disable(awi->vio_vreg);
1156 + mutex_lock(&awi->i2c_lock);
1157 + ret = aw9523_hw_init(awi);
1158 + mutex_unlock(&awi->i2c_lock);
1163 + mutex_destroy(&awi->i2c_lock);
1166 +static const struct i2c_device_id aw9523_i2c_id_table[] = {
1167 + { "aw9523_i2c", 0 },
1170 +MODULE_DEVICE_TABLE(i2c, aw9523_i2c_id_table);
1172 +static const struct of_device_id of_aw9523_i2c_match[] = {
1173 + { .compatible = "awinic,aw9523-pinctrl", },
1175 +MODULE_DEVICE_TABLE(of, of_aw9523_i2c_match);
1177 +static struct i2c_driver aw9523_driver = {
1179 + .name = "aw9523-pinctrl",
1180 + .of_match_table = of_aw9523_i2c_match,
1182 + .probe = aw9523_probe,
1183 + .remove = aw9523_remove,
1184 + .id_table = aw9523_i2c_id_table,
1186 +module_i2c_driver(aw9523_driver);
1188 +MODULE_DESCRIPTION("Awinic AW9523 I2C GPIO Expander driver");
1189 +MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>");
1190 +MODULE_LICENSE("GPL v2");